/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import bean.LoginActionFormBean;
import bean.SearchSupplierBean;
import dao.ActivityHistoryCrud;
import dao.ProviderDao;
import java.sql.SQLException;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.EditProvider;
import model.LogFile;
import model.OSDetector;
import model.ScheduledTaskLinux;
import model.ScheduledTaskWindows;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pojo.Activities;
import pojo.ActivityHistory;
import pojo.Provider;
import pojo.Scheduledata;

/**
 *
 * @author A7med Kotb
 */
public class SearchSupplierAction extends org.apache.struts.actions.DispatchAction 
{

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
   
    public ActionForward Show(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {
        
//        System.out.println("hello schedule task");
//        SearchSupplierBean apb=(SearchSupplierBean)form;
//        String providerName=apb.getProviderName();
        String providerName=request.getParameter("name");
        EditProvider ep=new EditProvider(getDataSource(request, "MyDS").getConnection());
        Provider pro=ep.generalInfo(providerName);
        Scheduledata sc=ep.scheduleInformation(pro.getPId());
        System.out.println("Schedule Task ID"+ sc.getScId());
       
        request.setAttribute("scheduledata", sc);
        return mapping.findForward("show");
    }
    
   
    public ActionForward Display(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {
        
        //System.out.println("hello schedule task");
        //SearchSupplierBean apb=(SearchSupplierBean)form;
        //String providerName=apb.getProviderName();
        EditProvider ep=new EditProvider(getDataSource(request, "MyDS").getConnection());
        //Provider pro=ep.generalInfo(providerName);
        //Scheduledata sc=ep.scheduleInformation(pro.getPId());
        //System.out.println("Schedule Task ID"+ sc.getScId());
       
        //request.setAttribute("scheduledata", sc);
        //return mapping.findForward("show");
        Vector<Provider> myvect=ep.getAll();
        request.setAttribute("Names", myvect);             
        return mapping.findForward("display");
    } 
    
    //method to stop running schedule task
    public ActionForward Disable(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {
        SearchSupplierBean ssb=(SearchSupplierBean)form;
        LoginActionFormBean loginbean=(LoginActionFormBean) request.getSession().getAttribute("LoginActionFormBean");
        Provider pro=new ProviderDao(getDataSource(request, "MyDS").getConnection()).getById(ssb.getScId());
        ActionForward forward=new ActionForward();
        boolean stopresult=false;
        if(OSDetector.isWindows()){
                stopresult=new ScheduledTaskWindows(getDataSource(request, "MyDS").getConnection()).disable(ssb.getScId());
        }
        if(OSDetector.isUnix()){
            stopresult=new ScheduledTaskLinux(getDataSource(request, "MyDS").getConnection()).disable(ssb.getScId());
        }
        boolean logresult=activityHistory("Stop Schedule Task", request, ssb.getScId());
        if(logresult == true)
        {
            LogFile.log("System","Logged the operation of Stopping the schedule task of the provider "+pro.getName()+" manually at "+ new Date());
        }
        else
        {
            LogFile.log("System","failed to log the operation of stopping the schedule task of the provider "+pro.getName()+" manually at "+ new Date());
        }
        if(stopresult == true)
        {
            LogFile.log(loginbean.getUserName(),"stopped schedule task of the provider "+pro.getName()+" manaually at "+new Date());
        }
        else
        {
            LogFile.log("System","failed to stop schedule task of the provider "+pro.getName()+" manually at "+ new Date());
        }
        return forward= Display(mapping, form, request, response);
    }
    
    //method to start schedule task now
    public ActionForward Start(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {
        ActionForward forward=new ActionForward();
        SearchSupplierBean ssb=(SearchSupplierBean)form;
        LoginActionFormBean loginbean=(LoginActionFormBean) request.getSession().getAttribute("LoginActionFormBean");
        Provider pro=new ProviderDao(getDataSource(request, "MyDS").getConnection()).getById(ssb.getScId());
        boolean startresult=false;
        if(OSDetector.isWindows()){
                startresult=new ScheduledTaskWindows(getDataSource(request, "MyDS").getConnection()).start(ssb.getScId());
        }
        if(OSDetector.isUnix()){
            startresult=new ScheduledTaskLinux(getDataSource(request, "MyDS").getConnection()).start(ssb.getScId());
        }
        boolean logresult=activityHistory("Start Schedule Task", request, ssb.getScId());
        if(logresult == true)
        {
            LogFile.log(" System "," Logged the operation of starting the schedule task of the provider "+pro.getName());
        }
        else
        {
            LogFile.log(" System "," failed to log the operation of disabling the schedule task of the provider "+pro.getName());
        }
        if(startresult == true)
        {
            LogFile.log(loginbean.getUserName(),"started schedule task of the provider "+pro.getName()+" manaually at "+new Date());
        }
        else
        {
            LogFile.log("System","failed to Start schedule task of the provider "+pro.getName()+" manually at "+ new Date());
        }
        return forward=Display(mapping, form, request, response);
    }
    
    //method to restart one of the schedule tasks now
    public ActionForward Enable(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception 
    {
        ActionForward forward=new ActionForward();
        SearchSupplierBean ssb=(SearchSupplierBean)form;
        LoginActionFormBean loginbean=(LoginActionFormBean) request.getSession().getAttribute("LoginActionFormBean");
        Provider pro=new ProviderDao(getDataSource(request, "MyDS").getConnection()).getById(ssb.getScId());
        boolean resumeresult=false;
        if(OSDetector.isWindows()){
                resumeresult=new ScheduledTaskWindows(getDataSource(request, "MyDS").getConnection()).enable(ssb.getScId());
        }
        if(OSDetector.isUnix()){
            resumeresult=new ScheduledTaskLinux(getDataSource(request, "MyDS").getConnection()).enable(ssb.getScId());
        }
        boolean logresult=activityHistory("Resume Schedule Task", request, ssb.getScId());
        if(logresult == true)
        {
            LogFile.log(" System "," Logged the operation of resuming the schedule task of the provider "+pro.getName());
        }
        else
        {
            LogFile.log(" System "," failed to log the operation of resuming the schedule task of the provider "+pro.getName());
        }
        if(resumeresult == true)
        {
            LogFile.log(loginbean.getUserName()," resumed the schedule task of the provider "+pro.getName()+" manaually at "+new Date());
        }
        else
        {
            LogFile.log("System","failed to resume the schedule task of the provider "+pro.getName()+" manually at "+ new Date());
        }
        return forward=Display(mapping, form, request, response);
    }
    
//Add Activity to Activity History
    public boolean activityHistory(String activityName ,HttpServletRequest request , int taskid)
    {
        boolean check=false;
        try 
        {
            LoginActionFormBean loginbean= (LoginActionFormBean) request.getSession().getAttribute("LoginActionFormBean");
            ActivityHistory log= new ActivityHistory();
            log.setAdminName(loginbean.getUserName());
            ProviderDao pdao=new ProviderDao(getDataSource(request, "MyDS").getConnection());
            Provider pro=pdao.getById(taskid);
            log.setProviderName(pro.getName());
            log.setStatus("Success");
            log.setLiveServer(null);
            ActivityHistoryCrud ahc = new ActivityHistoryCrud(getDataSource(request, "MyDS").getConnection());
            Activities act=ahc.getByName(activityName);
            log.setActivities(act);
            log.setActivityName(act.getName());
            check=ahc.add(log);
        }
        catch (SQLException ex) 
        {
            check=false;
            Logger.getLogger(STOperations.class.getName()).log(Level.SEVERE, null, ex);
        }
        return check;
    }
}
